package com.dfrz.stockMarket.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.dfrz.stockMarket.bean.KlineDataBean;
import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.bean.StockBean;
import com.dfrz.stockMarket.cons.Constants;
import com.dfrz.stockMarket.dao.StockDao;
import com.dfrz.stockMarket.dao.StockLogDao;
import com.dfrz.stockMarket.dao.UserStocksDao;
import com.dfrz.stockMarket.model.StockLogModel;
import com.dfrz.stockMarket.model.StocksModel;
import com.dfrz.stockMarket.model.UserStocksModel;
import com.dfrz.stockMarket.util.SortDate;
import com.dfrz.stockMarket.util.UtilTools;

public class StockService {
	/**
	 * 获得所有的股票
	 * @return
	 * @throws Exception 
	 */
	public static List getAllStock() throws Exception {
		List<StocksModel> stockBeans = new ArrayList<>();
		stockBeans=(List<StocksModel>) StockDao.getAllStock().getMsg();
		return stockBeans;
	}
	/**
	 * 获得单个股票得Kline数据
	 * @param ID股票ID
	 * @return 返回股票数据
	 * @throws Exception 
	 */
	public List<KlineDataBean> getKlineData(int ID) throws Exception{
		List<List<StockLogModel>> stockLog = getAllStocklogToDay(ID);
		List<KlineDataBean> klineData = new ArrayList<>();
		for (int i = 0; i < stockLog.size(); i++) {
			List<StockLogModel> cleanList = getSuccessStockLog(stockLog.get(i));
			if (cleanList.size() >= 1) {
				Date date  = cleanList.get(0).getCreateDate();
				Double open = 0.0;
				Double close = cleanList.get(cleanList.size()-1).getStockPrice();
				Double max = cleanList.get(0).getStockPrice();;
				Double min = cleanList.get(0).getStockPrice();;
				
				
				for (int i1 = 0; i1 < cleanList.size(); i1++) {
				 	if(min > cleanList.get(i1).getStockPrice()) min = cleanList.get(i1).getStockPrice();
					if (max < cleanList.get(i1).getStockPrice()) max = cleanList.get(i1).getStockPrice();		
				}		
				if (i-1>=0) {//判断当前是不是第一天
					int index = i-1; //如果不是第一天，则取前一天的下标
					open = getSuccessStockLog(stockLog.get(index)).get(0).getStockPrice();
				}
				KlineDataBean klineDataBean = new KlineDataBean(date, open, close, min, max);
				klineData.add(klineDataBean);
			}
			
		}
		return klineData;
	}
	/**
	 * 获得交易成功的股票日志
	 * @param list 单只股票的日志
	 * @return 
	 */
	private List<StockLogModel> getSuccessStockLog(List<StockLogModel> list){
		return getTypeStockLog(list, Constants.STOCK_TYPE_1);
	}
	/**
	 * 获得状态为卖出的股票日志
	 * @param list 单只股票的日志
	 * @return
	 */
	private List<StockLogModel> getSellStockLog(List<StockLogModel> list){
		return getTypeStockLog(list, Constants.STOCK_TYPE_2);
	}
	/**
	 * 获得状态为买入的股票日志
	 * @param list 单只股票的日志
	 * @return
	 */
	private List<StockLogModel> getBuyStockLog(List<StockLogModel> list){
		return getTypeStockLog(list, Constants.STOCK_TYPE_3);
	}
	/**
	 * 获得指定交易状态股票日志
	 * @param list 单只股票的日志
	 * @return
	 */
	private List<StockLogModel> getTypeStockLog(List<StockLogModel> list,int type){
		List<StockLogModel> list2 = new ArrayList<>();
		for (StockLogModel stockLogModel : list) {
			if (stockLogModel.getType() == type) {
				list2.add(stockLogModel);
			}
		}
		return list2;
	}
	
	/**
	 * 获得将单只股票日志按每日进行分类
	 * @param ID 需要获得的股票的代码
	 * @return 返回单只股票的每日日志
	 * @throws Exception 
	 */
	private List<List<StockLogModel>> getAllStocklogToDay(int ID) throws Exception{
		List<StockLogModel> stockLog = (List<StockLogModel>)StockLogDao.getStockLogByID(ID).getMsg();
		List<List<StockLogModel>> stocklogToDay = new ArrayList<>();
		Collections.sort(stockLog, new SortDate());
		
		StockLogModel stockLogStart = stockLog.get(0);
		StockLogModel stockLogend = stockLog.get(stockLog.size()-1);
		
		int i = UtilTools.daysOfTwo(stockLogStart.getCreateDate(), stockLogend.getCreateDate())+1;
		
		int index = 0;
		for (int j = 0; j < i; j++) {
			List<StockLogModel> list = new ArrayList<>();
			stocklogToDay.add(list);
		}
		for (StockLogModel stockLogModel : stockLog) {
			Date date = stockLogStart.getCreateDate();
			if ((UtilTools.cstToHMS(stockLogModel.getCreateDate(), "yyyy/MM/dd")).equals((UtilTools.cstToHMS(date, "yyyy/MM/dd")))) {
				stocklogToDay.get(index).add(stockLogModel);
			}else {
				stockLogStart = stockLogModel;
				index++;
				stocklogToDay.get(index).add(stockLogModel);
			}
		}

		for (int j = i-1; index < j; j--) {
			stocklogToDay.remove(j);
		}
		return stocklogToDay;
	}
	
	public List<StockLogModel> getAllStocklogToSell(int stockID) throws Exception{
		List<StockLogModel> stockLog = (List<StockLogModel>)StockLogDao.getStockLogByID(stockID).getMsg();
		List<StockLogModel> sellStockLog = getSellStockLog(stockLog);
		return sellStockLog;
	}
	public List<StockLogModel> getAllStocklogToBuy(int stockID) throws Exception{
		List<StockLogModel> stockLog = (List<StockLogModel>)StockLogDao.getStockLogByID(stockID).getMsg();
		List<StockLogModel> buyStockLog = getBuyStockLog(stockLog);
		return buyStockLog;
	}
	public MessageBean getStockByID(int stockID) throws Exception{
		
		return StockDao.getStockByID(stockID);
	}
	public MessageBean getAllUserStockCode(int ID) throws Exception{
		return UserStocksDao.getAllUserStockCodeById(ID);
	}
	
}
